package com.multimedia.scala.euler

import scala.collection.mutable.ListBuffer
import scala.annotation.tailrec
object Asd62 {
    def main(args : Array[String]) : Unit = {
        @tailrec
        def combinations(n: Int = 7) {
            def find_min(n: Int = n, acc: Long = 1): Long = {
                if (n == 0) return acc
                find_min(n-1, acc*10)
            }
            def find_max(n: Int = n, acc: Long = 9): Long = {
                if (n==0) return acc
                find_max(n-1, acc*10 + 9)
            }
            
            val min = Math.pow(find_min(), 1.0 / 3.0).floor.longValue + 1
            val max = Math.pow(find_max(), 1.0 / 3.0).floor.longValue

            val rez = scala.collection.mutable.Map[Long, Int]().withDefaultValue(0)
            val rez2 = scala.collection.mutable.Map[Long, Long]().withDefaultValue(Math.MAX_LONG)

            @tailrec
            def next(x: Long = min) {
                if (x>max) return
                //println(x*x*x)
                val cube = x*x*x
                val digit = cube.toString().toCharArray.sortWith{case (x, y)=>x>y}
                .map(_.toString).reduce(_+_).toLong
                println(digit)
                rez(digit) += 1
                if (cube<rez2(digit)) rez2(digit)=cube
                next(x+1)
            }
            next()
            //println(n+" = "+rez.values.max)
            //println(rez(66543210))
            //println(rez.first._1.toList)
            //if (n==7) return
            if (rez.values.max==5) {
                rez
                .filter{case(x,y)=>y>=5}
                .foreach{case (x, y)=>{
                    println(x, y)
                    println(rez2(x))
                }}
                return
            } else {
                return combinations(n + 1)
            }
        }
        combinations()
        //println(Array(3,2,4,1).sort{case (x, y)=>x>y})
        //println(Math.MAX_LONG)
    	//println(perm.permutations.size)
    }
}
